package com.yuexinduo.education.ui;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.android.material.tabs.TabLayout;
import com.yuexinduo.education.R;
import com.yuexinduo.baselibrary.base.BaseMvpActivity;
import com.yuexinduo.education.fragment.OrderAwaitFragment;
import com.yuexinduo.education.fragment.OrderEvaluateFragment;
import com.yuexinduo.education.fragment.OrderFinishFragment;

import java.util.ArrayList;
import java.util.List;

public class MyOrderActivity extends BaseMvpActivity {
    private ImageView back;
    private TextView top;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private List<String> tabList = new ArrayList<>();
    private List<Fragment> viewList = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected int bindLayout() {
        return R.layout.activity_my_order;
    }

    @Override
    protected Activity getActivity() {
        return this;
    }

    @Override
    protected void initCreate() {

        back = (ImageView) findViewById(R.id.back);
        top = (TextView) findViewById(R.id.top);
        tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        tabList.add("待完成");
        tabList.add("已完成");
        tabList.add("待评价");
        viewList.add(new OrderAwaitFragment());
        viewList.add(new OrderFinishFragment());
        viewList.add(new OrderEvaluateFragment());
        viewPager.setOffscreenPageLimit(2);

        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return viewList.get(position);
            }

            @Override
            public int getCount() {
                return viewList.size();
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return tabList.get(position);
            }
        });

        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                TextView textView = new TextView(getActivity());
//                float selectedSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 14, getResources().getDisplayMetrics());
//                textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,selectedSize);
                textView.setTextColor(getResources().getColor(R.color.black));
                textView.setText(tab.getText());
                tab.setCustomView(textView);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                tab.setCustomView(null);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        tabLayout.setupWithViewPager(viewPager);//此方法就是让tablayout和ViewPager联动
    }
}